export const baseUrl = 'https://tianleletao.com:6001/api'
// export const baseUrl = 'http://175.6.32.63:6101/api'
// export const baseUrl = 'https://140.246.101.8/api'
// 获取Token	

function getToken() {
    try {
        const token = uni.getStorageSync('token');
        if (token) {
            return token
        } else {
            return ''
        }
    } catch (e) {
        console.log(e)
    }
}

// 获取请求头
function getHeaders() {
    let token = getToken()
    let header = {
        "Authorization": token? 'Bearer '+token:'',
        "Content-Type": "application/json; charset=utf-8", //根据自己的数据类型
        // "Content-Type":"application/x-www-form-urlencoded",
    }
    return header
}

let request = function(data) {
    data.method = data.method.toUpperCase()
    // if (!['GET', 'POST'].includes(data.method)) {
    //     uni.showToast({
    //         title: `暂不支持的请求方式: ${data.method}`,
    //         icon: 'none'
    //     });
    //     return
    // }

    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + data.url,
            method: data.method,
            data: data.params,
            header: getHeaders(),
        }).then(res => {
			let  statusCode = res[1] && res[1].statusCode? res[1].statusCode:500;
            switch (statusCode) {
                case 200:
                    resolve(res[1]['data'])
                    break
                case 500:
                    resolve(res[1]['data'])
                    break
                default:
					resolve(res[1]['data'])
            }
        }).catch(
            (response) => {
                reject(response)
            }
        )
    })
}

export default  request;
